﻿using System;
using System.Collections.Generic;
using FineUI;
using System.Web;
using System.Web.UI;
using SDLITS.Common;
using SDLITS.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data.SqlClient;
using System.Data;


namespace EmptyProjectNet20
{
    public partial class _default : CommonBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 加载修改密码事件
                ChangePwd.OnClientClick = Window1.GetShowReference("Personal/ChangePwd.aspx", "修改密码");
                #endregion
                #region 日期礼拜
                string week = DateTime.Now.DayOfWeek.ToString();
                switch (week)
                {
                    case "Monday":
                        week = "星期一";
                        break;
                    case "Tuesday":
                        week = "星期二";
                        break;
                    case "Wednesday":
                        week = "星期三";
                        break;
                    case "Thursday":
                        week = "星期四";
                        break;
                    case "Friday":
                        week = "星期五";
                        break;
                    case "Saturday":
                        week = "星期六";
                        break;
                    case "Sunday":
                        week = "星期日";
                        break;
                }
                TText.Text = "今天是：" + DateTime.Now.ToString("yyyy年MM月dd日") + "&nbsp;&nbsp;&nbsp;<b>" + week + "</b>";
                #endregion
                #region 当前用户
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                ToolbarText1.Text = "当前登录用户：<b>" + (string.IsNullOrEmpty(user.RealName) ? user.UserName : user.RealName) + "</b>" + "(<font style='color:red;font-weight:bold;'>" + user.DepartmentName + "</font>)";
                #endregion
                setMenu();
                BindGrid();
                InitThemeMenuButton();
                setServerRunTime();

            }
        }

        protected void setServerRunTime()
        {
            Label1.Text = "<img src='/icon/exclamation.png' />";
            string m =
                    string.Format(
                        "服务器已正常运行 <span style='color: red;'>{0}</span> 小时",
                        Environment.TickCount / 3600000);

            if (Session["LoginUser"] != null)
            {
                var modelLoginUser = Session["LoginUser"] as ModelLoginUser;

                if (modelLoginUser != null && modelLoginUser.DeptID != -1)
                {
                    string errMsg;
                    SqlParameter[] sp = new SqlParameter[]{
                            new SqlParameter("@CreateUserID",modelLoginUser.UserID),
                            new SqlParameter("@StartDate",DateTime.Today),
                            new SqlParameter("@EndDate",DateTime.Today.AddDays(1)),
                            new SqlParameter("@SPAction","SelectTotalByDateRange")
                        };
                    DataSet ds = SDDbSession.Default.FromProc("TableCardRevenue").AddParameter(sp).ToDataSet();

                    if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow dr = ds.Tables[0].Rows[0];
                        decimal initialAmount = dr["InitialAmount"] is DBNull
                                                    ? 0
                                                    : Convert.ToDecimal(dr["InitialAmount"].ToString());
                        decimal workAmount = dr["WorkAmount"] is DBNull
                                                 ? 0
                                                 : Convert.ToDecimal(dr["WorkAmount"].ToString());
                        decimal bonusAmount = dr["BonusAmount"] is DBNull
                                                  ? 0
                                                  : Convert.ToDecimal(dr["BonusAmount"].ToString());
                        decimal total = dr["TotalAmount"] is DBNull
                                            ? 0
                                            : Convert.ToDecimal(dr["TotalAmount"].ToString());

                        m = m + "&nbsp;&nbsp;&nbsp;&nbsp;今日收费总金额为：" + (initialAmount + workAmount + bonusAmount + total).ToString() + " 元";
                    }
                }

            }
            runTimeLbl.Text = m;
            lblSysInfo.Text = "&nbsp;&nbsp;&nbsp;&nbsp;Version:1.0&nbsp;&nbsp;&nbsp;&nbsp;Copyright © HuoTuTu 2014";
        }

        protected void LogOut(object sender, EventArgs e)
        {
            Session.Clear();
            Session.Abandon();
            Response.Redirect("login.aspx");
        }
        protected void setMenu()
        {
            ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
            List<V_RoleMenu> items = SDDbSession.Default.From<V_RoleMenu>().Where(V_RoleMenu._.RoleID == user.RoleID && V_RoleMenu._.IsShow == true && V_RoleMenu._.ParentID == 0).ToList();
            Accordion Accordion1 = new Accordion();
            Accordion1.ShowHeader = false;
            Accordion1.ShowBorder = false;
            Accordion1.BodyPadding = "2px";
            Accordion1.Layout = Layout.Fit;
            SetModel(items, Accordion1);
            Region2.Items.Add(Accordion1);
        }
        protected void SetModel(List<V_RoleMenu> list, Accordion ad)
        {
            ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
            List<V_RoleMenu> items = SDDbSession.Default.From<V_RoleMenu>().Where(V_RoleMenu._.RoleID == user.RoleID && V_RoleMenu._.IsShow == true).ToList();
            int i = 0;
            foreach (V_RoleMenu modelitem in list)
            {
                List<V_RoleMenu> childlist2 = items.FindAll(delegate(V_RoleMenu tm) { return tm.ParentID == modelitem.MenuID && tm.MenuID != 391; });
                Tree tr = new Tree();
                tr.ShowHeader = false;
                tr.ShowBorder = false;
                tr.EnableLines = true;
                tr.ID = "SysMenu_" + i;
                foreach (V_RoleMenu childitem in childlist2)
                {
                    List<V_RoleMenu> treecount = items.FindAll(delegate(V_RoleMenu tm) { return tm.ParentID == childitem.MenuID; });
                    #region 动态添加树
                    TreeNode tn = new TreeNode();
                    tn.Text = childitem.MenuName;
                    tn.IconUrl = childitem.IconUrl;
                    //tn.IconUrl="/"
                    if (treecount.Count == 0)
                    {
                        if (childitem.MenuID != 76 && childitem.MenuID != 66)
                        {
                            tn.OnClientClick = "javascript:addUTab('" + childitem.WebUrl + "Manage.aspx','tab_" + childitem.MenuID + "','" + childitem.MenuName + "','" + childitem.IconUrl + "');";
                        }
                        else
                        {
                            tn.OnClientClick = "javascript:addUTab('" + childitem.WebUrl + ".aspx','tab_" + childitem.MenuID + "','" + childitem.MenuName + "','" + childitem.IconUrl + "');";
                        }
                    }
                    tr.Nodes.Add(tn);
                    ResolveSubTree1(tn, treecount);
                    #endregion
                }
                #region 添加Plan
                AccordionPane ap = new AccordionPane();
                ap.AutoScroll = true;
                ap.IconUrl = modelitem.IconUrl;
                ap.Title = "<span>" + modelitem.MenuName + "</span>";
                ap.Items.Add(tr);
                ad.Panes.Add(ap);
                i++;
                #endregion
            }
        }
        private void ResolveSubTree1(TreeNode treeNode, List<V_RoleMenu> dt)
        {
            if (dt.Count > 0)
            {
                treeNode.Expanded = true;
                foreach (V_RoleMenu childitem in dt)
                {
                    List<V_RoleMenu> treecount = dt.FindAll(delegate(V_RoleMenu tm) { return tm.ParentID == childitem.MenuID; });
                    #region 动态添加树
                    TreeNode tn = new TreeNode();
                    tn.Text = childitem.MenuName;
                    if (treecount.Count == 0)
                    {
                        tn.OnClientClick = "javascript:addUTab('" + childitem.WebUrl + ".aspx','tab_" + childitem.MenuID + "','" + childitem.MenuName + "','"+childitem.IconUrl+"');";
                    }
                    treeNode.Nodes.Add(tn);
                    ResolveSubTree1(tn, treecount);
                    #endregion
                }
            }
        }
        private void BindGrid()
        {
            Grid1.RecordCount = SDDbSession.Default.From<V_AnnounceUser>().Count();
            List<V_AnnounceUser> items = SDDbSession.Default.From<V_AnnounceUser>().Where(V_AnnounceUser._.StatusID == 1 && V_AnnounceUser._.EndDate > DateTime.Now).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();
            Grid1.DataSource = items;
            Grid1.DataBind();
        }
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string id = e.Values[6].ToString();
            e.Values[6] = "<a href=\"javascript:;\" onclick=\"F('Window2').f_show('Personal/DeskDetail.aspx?id=" + id + "','" + e.Values[1].ToString() + "');\"><span style=\"color:#157FCC;\">查看</span></a>";
        }

        protected void drp_theme_SelectedIndexChanged(object sender, EventArgs e)
        {
            HttpCookie cookie = new HttpCookie("theme_v4", drp_theme.SelectedValue);
            cookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(cookie);

            PageContext.Refresh();
        }
        private void InitThemeMenuButton()
        {
            HttpCookie themeCookie = Request.Cookies["theme_v4"];
            string themeValue = PageManager1.Theme.ToString().ToLower();
            if (themeCookie != null)
            {
                themeValue = themeCookie.Value;
            }
            drp_theme.SelectedValue = themeValue;
        }
    }
}